package spider;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class JobList {

    private static final String BASE_URL = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json?scene=1&query=&city=101080100" +
            "&experience=&payType=&partTime=&degree=&industry=&scale=&stage=&position=100101" +
            "&jobType=&salary=&multiBusinessDistrict=&multiSubway=&page=1&pageSize=30";

    public static void main(String[] args) {
        JobList a = new JobList();
        a.getJobList();
    }

    public String getJobList() {
//        long timestamp = System.currentTimeMillis();
//        String fileName = "ruoyi-hzhtest/src/main/resources/job_list/job_list_" + timestamp + ".txt";
        String fileName = "ruoyi-hzhtest/src/main/resources/job_list/job_list.txt";
        System.out.println("fileName: " + fileName);
        try {
            // 创建URL对象
            URL url = new URL(BASE_URL);

            // 创建HttpURLConnection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 设置请求头
            connection.setRequestProperty("accept", "application/json, text/plain, */*");
            connection.setRequestProperty("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
            connection.setRequestProperty("cookie", "lastCity=101080100; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1718278792; __g=-; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1718278797; wd_guid=52010551-b1c1-4347-af34-714fcfdfd09c; historyState=state; __zp_stoken__=d95efw4TClcK9DD1TCgtdAwTCtsK4bUtsT0TCt8KzcsK9blPCssKmfsKyYMKiwrXCksK%2BwpFJwr%2FCrcKdwqbChlDCmmrCpMK3w7HCrcK%2BTsKcwpLCvMKrwpPEn8O1wpfFr8OZwpjCucKRMS0EBBANCQQEEA0JDg4BBAgHBwsKDhAQBAEFPibClGM1PzQ1JktCQgdKXFhJXEYCXkpIODhdXQ4BOCM5NTg0wrPClsKyw4vCusKWwr7DisK%2Bwp%2FCvRY1QDQ5w4DCoyMiw4DCjCbCsQENw4DCmifCtMKBDT4Ew4RTV8OIwo10Jj8zwrjEszRAFz41QDI0MTxAQCcxOsOJU1vDhcKJVTA1ET1AQDszNUBAOTE7LEA9cyNAOSU9DQ8CDA4mM8K9QcK4w5dAQA%3D%3D; __c=1718278792; __a=38991715.1707373477.1707373477.1718278792.9.2.8.9");
            connection.setRequestProperty("priority", "u=1, i");
            connection.setRequestProperty("referer", "https://www.zhipin.com/web/geek/job?query=&city=101080100&position=100101");
            connection.setRequestProperty("sec-ch-ua", "\"Microsoft Edge\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"");
            connection.setRequestProperty("sec-ch-ua-mobile", "?0");
            connection.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
            connection.setRequestProperty("sec-fetch-dest", "empty");
            connection.setRequestProperty("sec-fetch-mode", "cors");
            connection.setRequestProperty("sec-fetch-site", "same-origin");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0");
            connection.setRequestProperty("x-requested-with", "XMLHttpRequest");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应
                // 读取响应
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 打印响应
                System.out.println("Response: " + response.toString());

                // 解析JSON响应
                JSONObject jsonResponse = new JSONObject(response.toString());
                JSONObject data = jsonResponse.getJSONObject("zpData");
                JSONArray jobList = data.getJSONArray("jobList");

                try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
                    // 遍历并打印职位信息
                    for (int i = 0; i < jobList.length(); i++) {
                        JSONObject job = jobList.getJSONObject(i);
                        // 写入文件
                        writer.write(job.toString());
                        writer.newLine();
                    }
                    System.out.println("职位信息已成功写入 job_list.txt 文件。");
                }
            } else {
                System.out.println("GET请求未成功。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}

